<?php
/**
 * User: Administrator
 * Note: 后台：登录、退出、图形验证
 */
namespace Admin\Controller;

use Think\Controller;

class PublicController extends Controller
{
    // 后台登录
    public function login()
    {
        if (IS_POST) {
            $username = I('username', '', 'trim');
            $password = I('password', '', 'trim');
            $vercode  = I('vercode', '', 'trim');
            if (!$username) {
                appReturn(0, '请输入用户名');
            }
            if (!$password) {
                appReturn(0, '请输入密码');
            }
            if (!$vercode) {
                appReturn(0, '请输入验证码');
            }
            $code = session('admin_validateCode');
            if ($code != $vercode) {
                appReturn(0, '验证码不正确');
            }
            session('admin_validateCode', null);
            $member = D('Member')->getInfoByName($username);

            if ($member['password'] != passwordMd5($password)) {
                appReturn(0, '密码不正确');
            }
            session('admin_uid', $member['id']);

            // 记录登陆日志
            D('LogAdmin')->writeAdmin($member['id'], time(), get_client_ip());

            /*if($member['password'] == passwordMd5('123456'))
            {
                session('admin_changePassword',1);
                appReturn(1,'管理员密码为初始密码<br>请在登录后尽快修改密码','',U('Config/modadmin'));
            }*/
            session('admin_changePassword', null);
            appReturn(1, '登陆成功', '', U('Index/index'));
        } else {
            $this->assign('meta_title', '登陆');
            $this->display();
        }
    }
    //  退出登录
    public function logout()
    {
        session('admin_uid', null);
        session_destroy();
        if (IS_AJAX) {
            appReturn(1, '注销成功');
        } else {
            redirect(U('Public/login'));
        }
    }

    // 获取图片验证码
    public function getCode()
    {
        vendor('ValidateCode');
        $ValidateCode = new \ValidateCode();
        $ValidateCode->doimg();
        // 记录验证码
        session('admin_validateCode', $ValidateCode->getCode());
    }
}
